#include "lvgl.h"
#include "GUI_Input_Component.h"
#include "GUI_Start.h"
#include <string.h>

/*
    ps:输入组件，用来进行数值输入的
*/

//点击键盘按钮0~9
void GUI_CharBtn_ClickEvent(lv_event_t * event){
    const char * str, *str1;
    lv_obj_t * btnLabel =lv_obj_get_child(event->target,0);
    str = lv_label_get_text(btnLabel);//获取按钮上的值
    lv_obj_t * input = lv_obj_get_parent(lv_obj_get_parent(event->target));
    lv_obj_t * targetTextarea = input->user_data;//输入的目标文本框框
    lv_obj_t * inputTextarea = lv_obj_get_child(lv_obj_get_child(input,0),0);//组件自带的文本框
    str1 = lv_textarea_get_text(inputTextarea);
    if(strchr(str1,'.')){//判断是否有小数点
        uint8_t posi = (strchr(str1,'.')-str1);//小数点位置
        if((strlen(str1)-posi)>=2){//只能输入1位小数
            return;
        }
    }
    //进行数字规范
    if(strcmp(str1,"0") == 0){//字符框为0
        if(strcmp(str,"0") != 0){//如果输入的不是0
            lv_textarea_set_text(inputTextarea,str);
        }
    }else{
        lv_textarea_add_text(inputTextarea,str);
    }
    //lv_textarea_add_text(inputTextarea,str);
    if(targetTextarea){
        lv_obj_add_state(inputTextarea,LV_STATE_FOCUSED);
        lv_textarea_set_text(targetTextarea,lv_textarea_get_text(inputTextarea));
    }
}
//小数点按钮
void GUI_PointBtn_ClickEvent(lv_event_t * event){
    lv_obj_t * input = lv_obj_get_parent(lv_obj_get_parent(event->target));
    lv_obj_t * inputTextarea = lv_obj_get_child(lv_obj_get_child(input,0),0);//组件自带的文本框
    lv_obj_t * targetTextarea = input->user_data;//输入的目标文本框框
    const char * str = lv_textarea_get_text(inputTextarea);
    if(strchr(str,'.')){
        return;
    }
    lv_textarea_add_char(inputTextarea,'.');
}
//删除字符按钮
void GUI_DeleteCharBtn_ClickEvent(lv_event_t * event){
    lv_obj_t * input = lv_obj_get_parent(lv_obj_get_parent(event->target));
    lv_obj_t * inputTextarea = lv_obj_get_child(lv_obj_get_child(input,0),0);//组件自带的文本框
    lv_obj_t * targetTextarea = input->user_data;//输入的目标文本框框
    const char * str = lv_textarea_get_text(inputTextarea);
    if(strcmp(str,"0") != 0){//不为0
        if(strlen(str) == 1){//且长度为1
            lv_textarea_set_text(inputTextarea,"0");
        }else{
            lv_textarea_del_char(inputTextarea);
        }
    }
    if(targetTextarea){
        lv_obj_add_state(inputTextarea,LV_STATE_FOCUSED);
        lv_textarea_set_text(targetTextarea,lv_textarea_get_text(inputTextarea));
    }
}
//设置字符输入的textarea
void GUI_InputNum_SetInputTarget(lv_obj_t * inputNum,lv_obj_t * textarea){
    if(inputNum->user_data != NULL){
        lv_obj_clear_state(inputNum->user_data,LV_STATE_FOCUSED);
    }
    inputNum->user_data = textarea;
    lv_obj_t * inputNumText = lv_obj_get_child(lv_obj_get_child(inputNum,0),0);//获取input组件中的文本框
    lv_textarea_set_text(inputNumText,lv_textarea_get_text(textarea));//将目标文本框的内容设置到input组件中的文本框去
}
//设置关闭函数
void GUI_InputNum_SetCloseEvent(lv_obj_t * inputNum,lv_event_cb_t cb){
    lv_obj_get_child(inputNum,0)->user_data = cb;
}
//设置键盘关闭回调函数
void GUI_InputNum_ClickCloseEvent(lv_event_t * event){
    lv_obj_t * btn = event->target;
    lv_obj_t * inputNum = lv_obj_get_parent(lv_obj_get_parent(event->target));
    event->target = lv_obj_get_parent(lv_obj_get_parent(btn));
    if(inputNum != NULL){
        if(lv_obj_get_child(inputNum,0)->user_data){//设置了关闭回调函数
            ((lv_event_cb_t)(lv_obj_get_child(inputNum,0)->user_data))(event);//执行设置的回调函数
        }
        if(inputNum->user_data){//如果输入框还绑定了键盘
            lv_obj_clear_state(inputNum->user_data,LV_STATE_FOCUSED);//清除输入框的聚焦
        }
    }
}
//该组件用于创建输入数值的框
lv_obj_t * GUI_Create_InputNum(lv_obj_t * father){
    uint8_t i;
    static lv_style_t btn_Style;
    lv_style_init(&btn_Style);
    lv_style_set_bg_color(&btn_Style,lv_color_hex(0xF3F3F3));
    lv_style_set_text_color(&btn_Style,lv_color_hex(0x333333));
    lv_style_set_width(&btn_Style,INPUTNUM_WIN_WIDTH/4-8);
    lv_style_set_height(&btn_Style,(INPUTNUM_WIN_HEIGHT-30)/3-8);
    //组件容器
    lv_obj_t * inputWin = lv_obj_create(father);
    lv_obj_set_size(inputWin,INPUTNUM_WIN_WIDTH,INPUTNUM_WIN_HEIGHT);
    lv_obj_set_style_border_width(inputWin,0,LV_STATE_DEFAULT);
    lv_obj_set_style_pad_all(inputWin,0,LV_STATE_DEFAULT);
    //文本框区域
    lv_obj_t * text = lv_obj_create(inputWin);
    lv_obj_set_style_border_width(text,0,LV_STATE_DEFAULT);
    lv_obj_set_style_pad_all(text,0,LV_STATE_DEFAULT);
    lv_obj_set_style_radius(text,0,LV_STATE_DEFAULT);
    lv_obj_set_width(text,INPUTNUM_WIN_WIDTH);
    lv_obj_set_height(text,30);
    lv_obj_t * textarea = lv_textarea_create(text);
    lv_obj_set_size(textarea,INPUTNUM_WIN_WIDTH-80,28);
    lv_obj_set_style_pad_all(textarea,4,LV_STATE_DEFAULT);
    lv_obj_align(textarea,LV_ALIGN_LEFT_MID,2,0);
    lv_obj_t * closeBtn = lv_btn_create(text);
    lv_obj_align(closeBtn,LV_ALIGN_RIGHT_MID,-2,0);
    lv_obj_set_size(closeBtn,70,28);
    lv_obj_add_event_cb(closeBtn,GUI_InputNum_ClickCloseEvent,LV_EVENT_CLICKED,NULL);
    lv_obj_t * closeBtnLabel = lv_label_create(closeBtn);
    lv_label_set_text(closeBtnLabel,"close");
    lv_obj_center(closeBtnLabel);

    //键盘
    lv_obj_t * numKeyboard = lv_obj_create(inputWin);
    numKeyboard->user_data = NULL;
    lv_obj_set_width(numKeyboard,INPUTNUM_WIN_WIDTH);
    lv_obj_set_height(numKeyboard,INPUTNUM_WIN_HEIGHT-30);
    lv_obj_set_style_border_width(numKeyboard,0,LV_STATE_DEFAULT);
    lv_obj_set_style_pad_all(numKeyboard,4,LV_STATE_DEFAULT);
    lv_obj_align(numKeyboard,LV_ALIGN_BOTTOM_MID,0,0);
    //采用用网格布局，记在lv_conf中开启
    static lv_coord_t column_dsc[] = {INPUTNUM_WIN_WIDTH/4-10, INPUTNUM_WIN_WIDTH/4-10,INPUTNUM_WIN_WIDTH/4-10,INPUTNUM_WIN_WIDTH/4 -10,LV_GRID_TEMPLATE_LAST};   /*2 columns with 100 and 400 ps width*/
    static lv_coord_t row_dsc[] = {(INPUTNUM_WIN_HEIGHT-30)/3-10, (INPUTNUM_WIN_HEIGHT-30)/3-10, (INPUTNUM_WIN_HEIGHT-30)/3-10, LV_GRID_TEMPLATE_LAST}; /*3 100 px tall rows*/
    lv_obj_set_grid_dsc_array(numKeyboard, column_dsc,row_dsc);

    //9个数字按钮
    lv_obj_t * btn[9];
    for(i=0;i<9;i++){
        btn[i] = lv_btn_create(numKeyboard);
        lv_obj_add_style(btn[i],&btn_Style,LV_STATE_DEFAULT);
        lv_obj_set_grid_cell(btn[i],LV_GRID_ALIGN_START,i%3,1,LV_GRID_ALIGN_START,i/3,1);
        lv_obj_t * btnLabel = lv_label_create(btn[i]);
        lv_label_set_text_fmt(btnLabel,"%d",i+1);
        lv_obj_center(btnLabel);
        lv_obj_add_event_cb(btn[i],GUI_CharBtn_ClickEvent,LV_EVENT_CLICKED,NULL);
    }
    lv_obj_t *btn2[3];//增加，减少，退出
    for(i=0;i<3;i++){
        btn2[i] = lv_btn_create(numKeyboard);
        lv_obj_add_style(btn2[i],&btn_Style,LV_STATE_DEFAULT);
        lv_obj_set_grid_cell(btn2[i],LV_GRID_ALIGN_START ,3,1,LV_GRID_ALIGN_START ,i%3,1);
        lv_label_create(btn2[i]);
        lv_obj_center(lv_obj_get_child(btn2[i],0));
    }
    lv_label_set_text(lv_obj_get_child(btn2[0],0),".");
    lv_obj_add_event_cb(btn2[0],GUI_PointBtn_ClickEvent,LV_EVENT_CLICKED,NULL);
    lv_label_set_text(lv_obj_get_child(btn2[1],0),"0");
    lv_obj_add_event_cb(btn2[1],GUI_CharBtn_ClickEvent,LV_EVENT_CLICKED,NULL);
    lv_label_set_text(lv_obj_get_child(btn2[2],0),"delete");
    lv_obj_set_style_bg_color(btn2[2],lv_color_hex(0xEE5555),LV_STATE_DEFAULT);
    lv_obj_add_event_cb(btn2[2],GUI_DeleteCharBtn_ClickEvent,LV_EVENT_CLICKED,NULL);
    return inputWin;
}
